import shutil
import time

class auto_cache(object):
    def __init__(self, myfile, cache_path="/tmp"):
        assert os.path.isfile(myfile)
        path, filename = os.path.split(myfile)
        self.cache_file = os.path.join(cache_path, filename)
        if not os.path.isfile(self.cache_file):
            print("Caching {} from {} to {}".format(filename, path, cache_path))
            start_time = time.perf_counter()
            shutil.copy(myfile, self.cache_file)
            end_time = time.perf_counter()
            print("Elapsed Time: {:.2f}s".format(end_time-start_time))
        else:
            print("Cache file {} been detected!".format(self.cache_file))

    def __enter__(self):
        return self.cache_file

    def __exit__(self, exc_type, exc_val, exc_tb):
        return True